package thread;

public class Join {
    public static class ThreadA extends Thread {
        public ThreadA() {
            super("ThreadA");
        }

        @Override
        public void run() {
            System.out.println("ThreadA run");
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("ThreadA: " + i);
            }
        }
    }

    public static class ThreadB extends Thread {
        ThreadA threadA=null;
        public ThreadB(ThreadA threadA) {
            super("ThreadB");
            this.threadA=threadA;
        }

        @Override
        public void run() {
            System.out.println("ThreadB run");
            try {
                threadA.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("ThreadB: " + i);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadA a =new ThreadA();
        ThreadB b =new ThreadB(a);
        a.start();
        b.start();
        b.join();
        System.out.println("main end");

    }
}
